<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

#获取页面变量
$action = trim($_GET['action']);
$id= trim($_REQUEST['id']) + 0;
	$quote['id'] = string_sql($id);
#如果有group_id
if ($id > 0) {
	$sql = "SELECT * FROM cards WHERE id=$id";
	$_card = $database->queryFirst($sql);
	if (!$_card['id']) {
		$_REQUEST['referrer'] = "index.php";
		redirect("记录不存在!");
	}
}
#得到post的值
$name = trim($_REQUEST['name']); 
	$quote['name'] = string_sql($name);
$price = trim($_REQUEST['price']);
	$quote['price'] = string_sql($price);
$memo = trim($_REQUEST['memo']);
	$quote['memo'] = string_sql($memo);
#如果是编辑
if ($action == "edit.form" || $action == "view.form") {
	$name = $_card['name'];
	$price = $_card['price'];
	$memo = $_card['memo'];
}
#如果是添加操作
if ($action == "add.do") {
	verify_field("name", "会员卡等级", "empty");
	verify_field("price", "价格", "empty");
	verify_field("memo", "服务内容", "empty");

	if (!count($error_strs)) {
		$sql = "INSERT INTO cards (name, price, memo) VALUES (".$quote['name'].", ".$quote['price'].", ".$quote['memo'].");";
		$database->execute($sql);		
		$_REQUEST['referrer'] = "index.php";
		redirect("添加会员卡成功!");
	} else {
		$action = "add.form";
	}
#如果是编辑操作
} else if ($action == "edit.do" && $id > 0) {

	verify_field("name", "会员卡等级", "empty");
	verify_field("price", "价格", "empty");
	verify_field("memo", "服务内容", "empty");

	if (!count($error_strs)) {
		$sql = "UPDATE cards SET name = ".$quote['name'].", price = ".$quote['price'].", memo = ".$quote['memo']." WHERE id = ".$quote['id'].";";
		$rs = $database->execute($sql);

		$_REQUEST['referrer'] = "index.php";
		redirect("修改会员卡成功!");
	} else {
		$action = "edit.form";
	}
#如果是删除操作
} else if ($action == "delete.do" && $id > 0) {
	
	$database->execute("DELETE FROM cards WHERE id = ".$quote['id'].";");
        $_REQUEST['referrer'] = "index.php";
        redirect("删除会员卡成功!");
}
#样式
$css = array();
$js = array("tiny_mce/tiny_mce");
#菜单
$menu = "card";
include_once("../layout/_header.php");
?>
<div class="main">
<div class="traininc-nav-list">
<ul>
<?php
if($action == "add.form"){
?>
<li><a href="index.php">会员卡列表</a></li>
<li class="selected"><a href="javascript:;">新增会员卡</a></li>
<?php
}elseif($action == "edit.form"){
?>
<li><a href="index.php">会员卡列表</a></li>
<li class="selected"><a href="javascript:;">修改会员卡</a></li>
<?php
}else{
?>
<li class="selected"><a href="javascript:;">会员卡详细</a></li>
<li><a href="edit.php?action=add.form">新增会员卡</a></li>
<?php
}
?>
</ul>
</div>
<script language="javascript" type="text/javascript">
  tinyMCE.init({
    language : "zh-cn",
    theme : "advanced",
    mode: "exact",
    elements : "memo",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,"
    + "justifyleft,justifycenter,justifyright,justifyfull,formatselect,"
    + "bullist,numlist,outdent,indent",
    theme_advanced_buttons2 : "link,unlink,anchor,image,separator,"
    +"undo,redo,cleanup,code,separator,sub,sup,charmap",
    theme_advanced_buttons3 : "",
    height:"300px",
    width:"600px"
});
</script>
<?
if ($action == "add.form") {
?>
<form action="edit.php?action=add.do" method="post" id="form" name="form">
<div class="box-con">
<table class="table-input">
        <tr>
                <td class="title">会员卡名称</td>
                <td><input name="name" type="text" class="text" size="30" value="<?= htmlspecialchars($name) ?>" > <?= show_field_error('name') ?></td>
        </tr>
        <tr>
                <td class="title">价格</td>
                <td><input name="price" type="text" class="text" size="30" value="<?= htmlspecialchars($name) ?>" > <?= show_field_error('price') ?></td>
        </tr>
        <tr>
                <td class="title">描述</td>
                <td><textarea name="memo" id="memo" cols="60" rows="7"><?= htmlspecialchars($memo) ?></textarea> <?= show_field_error('memo') ?></td>
        </tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="提交" class="btnA" /><input type="reset" name="" value="取消" class="btnB" /></td>
        </tr>
</table>
</div>
</form>
<?
} else if ($action == "edit.form") {
?>
<form action="edit.php?action=edit.do" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
<div class="box-con">
<table class="table-input">
        <tr>
                <td class="title">会员卡名称</td>
                <td><input name="name" type="text" class="text" size="30" value="<?= htmlspecialchars($name) ?>" > <?= show_field_error('name') ?></td>
        </tr>
        <tr>
                <td class="title">价格</td><?= show_field_error('price') ?>
                <td><input name="price" type="text" class="text" size="30" value="<?= htmlspecialchars($name) ?>" ></td>
        </tr>
        <tr>
                <td class="title">描述</td><?= show_field_error('memo') ?>
                <td><textarea name="memo" cols="60" rows="7"><?= htmlspecialchars($memo) ?></textarea></td>
        </tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="修改" class="btnA" /></td>
        </tr>
</table>
</div>
</form>
<?php
}else{
?>
<form action="edit.php?action=edit.form" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
<div class="box-con">
<table class="table-view">
        <tr>
                <td class="title">会员卡等级</td>
                <td><?= $name ?></td>
        </tr>
        <tr>
                <td class="title">价格</td>
                <td><?= $price ?></td>
        </tr>
        <tr>
                <td class="title">服务内容</td>
                <td><?= $memo ?></td>
        </tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="修改" class="btnA" /></td>
        </tr>
</table>
</div>
    </form>
<?php
}
?>
</div>
<?
include_once("../layout/_footer.php");
?>